package com.luojialong.onlineJudge.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.luojialong.onlineJudge.judge.codesandbox.model.ExecuteCodeResponse;
import com.luojialong.onlineJudge.model.dto.question.JudgeCase;
import com.luojialong.onlineJudge.model.dto.question.JudgeConfig;
import com.luojialong.onlineJudge.judge.codesandbox.model.JudgeInfo;
import com.luojialong.onlineJudge.model.entity.Question;
import com.luojialong.onlineJudge.model.entity.QuestionSubmit;
import com.luojialong.onlineJudge.model.enums.JudgeInfoMessageEnum;

import java.util.List;

public class DefaultJudgeStrategy implements JudgeStrategy {


    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext, ExecuteCodeResponse executeCodeResponse) {
        QuestionSubmit questionSubmit = judgeContext.getQuestionSubmit();
        if (executeCodeResponse.getMessage().equals(JudgeInfoMessageEnum.PARAM_ERROR)) {
            questionSubmit.setCodeStatus(JudgeInfoMessageEnum.PARAM_ERROR.getText());
            return judgeContext.getJudgeInfo();
        }
        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setMessage(executeCodeResponse.getJudgeInfo().getMessage());
        if (executeCodeResponse.getMessage() != null && executeCodeResponse.getMessage().contains("编译")) {
            questionSubmit.setCodeStatus(JudgeInfoMessageEnum.COMPILE_ERROR.getText());
            return judgeInfoResponse;
        }
        if (executeCodeResponse.getMessage() != null && executeCodeResponse.getMessage().contains("超时异常")) {
            questionSubmit.setCodeStatus(JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED.getText());
            return judgeInfoResponse;
        }
        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();
        Question question = judgeContext.getQuestion();
        Long memory = executeCodeResponse.getJudgeInfo().getMemory();
        Long time = executeCodeResponse.getJudgeInfo().getTime();
        JudgeInfoMessageEnum judgeInfoMessageEnum = JudgeInfoMessageEnum.ACCEPTED;
        judgeInfoResponse.setMemory(memory);
        judgeInfoResponse.setTime(time);
        if (outputList.size() != inputList.size()) {
            questionSubmit.setCodeStatus(JudgeInfoMessageEnum.WRONG_ANSWER.getText());
            return judgeInfoResponse;
        }
        for (int i = 0; i < judgeCaseList.size(); i++) {
            JudgeCase judgeCase = judgeCaseList.get(i);
            if (!judgeCase.getOutput().equals(outputList.get(i))) {
                judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
                questionSubmit.setCodeStatus(judgeInfoMessageEnum.getText());
                return judgeInfoResponse;
            }
        }
        String judgeConfigStr = question.getJudgeConfig();
        JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);

        long needMemoryLimit = judgeConfig.getMemoryLimit();
        long needTimeLimit = judgeConfig.getTimeLimit();

        if (memory > needMemoryLimit) {
            questionSubmit.setCodeStatus(JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED.getText());
            return judgeInfoResponse;
        }
        if (time > needTimeLimit) {
            questionSubmit.setCodeStatus(JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED.getText());
            return judgeInfoResponse;
        }
        questionSubmit.setCodeStatus(judgeInfoMessageEnum.getText());
        return judgeInfoResponse;
    }
}
